package com.example.myapplication.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;
import com.example.myapplication.ui.fragment.LiveListFragment;
import com.example.myapplication.util.ImageLoader;
import com.example.myapplication.util.StatusUtils;
import com.google.android.material.appbar.AppBarLayout;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private MagicIndicator magicIndicator;
    private ViewPager mViewPager;
    private String[] titles = {"视频", "喜欢"};
    private List<String> titleList;
    private final int normalColor = Color.parseColor("#999999");
    private final int selectedColor = Color.parseColor("#2F3635");
    private TextView tvOrder;
    private RelativeLayout rlUser;
    private TextView tvNicknameTop;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusUtils.setStatusBar(this);
        setContentView(R.layout.activity_main);
        titleList = Arrays.asList(titles);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusUtils.setAndroidNativeLightStatusBar(this, true);
        }

        magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
        tvNicknameTop = (TextView) findViewById(R.id.tv_nickname_top);
        rlUser = (RelativeLayout) findViewById(R.id.rl_user);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(LiveListFragment.newInstance());
        fragments.add(LiveListFragment.newInstance());

        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return titleList.size();
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        bindViewPager();
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder("您有 2 个订单正 待收货");
        stringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#1CAEA0")), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#1CAEA0")), 10, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(new AbsoluteSizeSpan(46), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(new AbsoluteSizeSpan(36), 10, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvOrder = findViewById(R.id.tv_order);
        tvOrder.setText(stringBuilder);
        loadAvatar();
        initListener();
    }

    private void initListener() {
        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.al);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) < rlUser.getHeight()) {
                    float rato = ((float) Math.abs(verticalOffset)) / rlUser.getHeight();
                    Log.d(TAG, "onOffsetChanged: "+rato);
                     tvNicknameTop.setAlpha(rato);
                } else {
                    tvNicknameTop.setAlpha(1);
                }
                Log.d(TAG, "onOffsetChanged: " + verticalOffset + ",rlUser:"+rlUser.getHeight()+",appBarLayout:" + appBarLayout.getTotalScrollRange());
            }
        });
    }

    private void loadAvatar() {
        ImageView ivAvatar = (ImageView) findViewById(R.id.iv_avatar);
        ImageLoader.loadCircleImage(this, "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3065179808,949830502&fm=11&gp=0.jpg", ivAvatar);
    }

    private void bindViewPager() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return titleList == null ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(normalColor);
                colorTransitionPagerTitleView.setSelectedColor(selectedColor);
                colorTransitionPagerTitleView.setText(titleList.get(index));
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(Color.parseColor("#38CDC3"), Color.parseColor("#15A394"), Color.parseColor("#129C8D"));
                indicator.setRoundRadius(5);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }

    /**
     * 处理首页的视差动画
     */
    private void handleAnim() {
        // 1,向上滚动时 iv_avatar，tv_Id 缩小且偏移直至最小,tv_nickname居中
        // 2，向下滚动时 iv_avatar，tv_Id tv_nickname恢复到原来大小和位置
    }


}